package api

import (
	"fmt"

	"jslog/example"
	"jslog/util"

	"strings"

	"jslog/common/model"

	"github.com/gin-gonic/gin"
)

type User struct {
	Base
}

func (s User) Edit(c *gin.Context) {

	c.Request.ParseMultipartForm(32 << 20)
	for k, v := range c.Request.PostForm {
		fmt.Println("-----------------------------------------------")
		fmt.Println(k, "--------->", v[0])
	}

	var field string = ""
	username := c.DefaultPostForm("username", "")
	if username != "" {
		field += "`username`='" + username + "',"
	}
	nickname := c.DefaultPostForm("nickname", "")
	if username != "" {
		field += "`nickname`='" + nickname + "',"
	}
	realname := c.DefaultPostForm("realname", "")
	if realname != "" {
		field += "`realname`='" + realname + "',"
	}
	sex := c.DefaultPostForm("sex", "")
	if sex != "" {
		field += "`sex`=" + sex + ","
	}
	age := c.DefaultPostForm("age", "")
	if age != "" {
		field += "age=" + age + ","
	}
	phone := c.DefaultPostForm("phone", "")
	if phone != "" {
		field += "`phone`='" + phone + "',"
	}
	email := c.DefaultPostForm("email", "")
	if email != "" {
		field += "`email`='" + email + "',"
	}
	avatar := c.DefaultPostForm("avatar", "")
	if avatar != "" {
		field += "`avatar`='" + avatar + "',"
	}
	field = strings.TrimRight(field, ",")
	if field != "" {
		uid := s.UserInfo(c)
		userModelObj := new(model.UserModel)
		tx := example.JSLOG_DB.Debug().Exec("UPDATE "+userModelObj.TabName()+" SET "+field+" WHERE id = ?", util.StrToUint64(uid))
		if tx.Error == nil {
			s.Succee("修改成功", c)
			return
		}
	}
	s.Error(0, "", "修改失败", c)
	return
}

func (s User) Detail(c *gin.Context) {
	fmt.Println("------------------------------------------------------------")
	fmt.Println("------------------------------------------------------------")
}
